package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.JingfeiPici;
import com.ruoyi.system.service.IDeptidOlddeptidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JingfeiKaifangMapper;
import com.ruoyi.system.domain.JingfeiKaifang;
import com.ruoyi.system.service.IJingfeiKaifangService;
import com.ruoyi.common.core.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-10
 */
@Service
public class JingfeiKaifangServiceImpl implements IJingfeiKaifangService 
{
    @Autowired
    private JingfeiKaifangMapper jingfeiKaifangMapper;

    @Autowired
    private JingfeiPiciServiceImpl jingfeiPiciService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public JingfeiKaifang selectJingfeiKaifangById(String id)
    {
        return jingfeiKaifangMapper.selectJingfeiKaifangById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param jingfeiKaifang 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<JingfeiKaifang> selectJingfeiKaifangList(JingfeiKaifang jingfeiKaifang)
    {
        return jingfeiKaifangMapper.selectJingfeiKaifangList(jingfeiKaifang);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param jingfeiKaifang 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertJingfeiKaifang(JingfeiKaifang jingfeiKaifang)
    {
        return jingfeiKaifangMapper.insertJingfeiKaifang(jingfeiKaifang);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param jingfeiKaifang 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateJingfeiKaifang(JingfeiKaifang jingfeiKaifang)
    {
        return jingfeiKaifangMapper.updateJingfeiKaifang(jingfeiKaifang);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteJingfeiKaifangByIds(String ids)
    {
        return jingfeiKaifangMapper.deleteJingfeiKaifangByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteJingfeiKaifangById(String id)
    {
        return jingfeiKaifangMapper.deleteJingfeiKaifangById(id);
    }

    @Override
    public boolean checkKaifangZJZX(String ppDeptId){
        JingfeiPici mmJingfeipici = new JingfeiPici();
        mmJingfeipici.setIsdangqianpici(1);
        mmJingfeipici.setDeptid("0");
        List<JingfeiPici> mmJingfeipiciList = jingfeiPiciService.selectJingfeiPiciList(mmJingfeipici);

        for (int i=0;i<mmJingfeipiciList.size();i++){
            Date now = new Date();

            String mmPiciId = mmJingfeipiciList.get(i).getId();
            JingfeiKaifang mmJingfeiKaifang = new JingfeiKaifang();
            mmJingfeiKaifang.setJingfeipiciid(mmPiciId);
            mmJingfeiKaifang.setDeptid(deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId));
            mmJingfeiKaifang.setIsvalid((long)1);
            List<JingfeiKaifang> mmKaifangList = selectJingfeiKaifangList(mmJingfeiKaifang);
            if (mmKaifangList.size()==1){
                Date mmKaifangshijian = mmKaifangList.get(0).getJiezhishijian();
                if (mmKaifangshijian.before(now)){
                    return false;
                }
            }else {
                Date mmJiezhiriqi = mmJingfeipiciList.get(i).getJisuanshijian();
                if (mmJiezhiriqi.before(now)){
                    return false;
                }
            }
        }
        return true;
    }
}
